var transLangFile = {
	data : {},
	module : '',
	url: '',
	init: function() {
		$('.module_lang_f').click (
			function() {
				tr = jQuery(this);
				if (tr.attr('src') == transLangFile.url +'images/icons/minus.gif') {
					tr.parents().first().find('.trans_lang').remove();
					tr.parents().first().css('height', 10);
					tr.attr('src', transLangFile.url +'images/icons/plus.gif');
				} else {
					var module_name = tr.parent().contents().text();
					var html = '';
					$.each(transLangFile.data[module_name], function(k, v) {
						// console.log(k);
						if (typeof v['no'] !== 'undefined')
							warning = ';color: red';
						else
							warning = '';
						html += '<div class="trans_lang" style="border: 1px solid red'+ warning +'"><img style="width: 10px; margin: 0 3px" class="lang_trans" src="'+ transLangFile.url +'images/icons/plus.gif" onclick="transLangFile.transLang.call(this)"><span class="lang">' + k + '</span></div>';
					});
					tr.attr('src', transLangFile.url +'images/icons/minus.gif');
					tr.parent().append(html);
				}
			}
		);
	},
	transLang: function() {
		div_lng = jQuery(this);
		if (div_lng.attr('src') == transLangFile.url +'images/icons/minus.gif') {
			div_lng.parents().first().find('.trans_file').remove();
			div_lng.attr('src', transLangFile.url +'images/icons/plus.gif');
			div_lng.parent().first().css('height', 15).css('margin', 0);
		} else {
			var lng = div_lng.parent().contents().text();
			var module_name = div_lng.parents('td').first().find('.module_trans').text();
			var i = 0;
			var html = '';
			$.each(transLangFile.data[module_name][lng], function(k, v) {
				if (k === 'no') {
					//Izbavitsja ot copy paste html koda
					$.each(v, function(ke, va) {
						html += '<div class="trans_file" style="height:20px; border: 1px solid red; color: red"><img style="width: 10px; margin: 0 3px" class="lang_file" src="'+ transLangFile.url +'images/icons/plus.gif" onclick="transLangFile.transFile.call(this)">' + va + '</div>';
						div_lng.css('color', 'red');
						i++;
					});
				} else {
					html += '<div class="trans_file" style="height:20px; border: 1px solid red; color: black"><img style="width: 10px; margin: 0 3px" class="lang_file" src="'+ transLangFile.url +'images/icons/plus.gif" onclick="transLangFile.transFile.call(this)">' + v + '</div>';
					i++;
				}
			});
			div_lng.attr('src', transLangFile.url +'images/icons/minus.gif');
			div_lng.parent().first().css('height', i * 40).css('margin', 5);
			div_lng.parent().first().append(html);
		}
	},
	transFile: function() {
		var file = jQuery(this);
		if (file.attr('src') == transLangFile.url +'images/icons/minus.gif') {
			$('.trans_item').remove();
			file.attr('src', transLangFile.url +'images/icons/plus.gif');
			file.parent().first().css('height', 150).css('margin', 3);
			tr.parent().css('height', 100).attr('valign', 'top');
			div_lng.parent().first().css('height', 200);
			file.parents('div').first().css('height', 20).css('margin', 2);
			$('.update_trans').remove();
			$('.addRow').remove();
		} else {
			var file_name = file.parents('div').first().text();
			var module_name = file.parents('td').first().find('.module_trans').text();
			var lang = file.parents().find('.lang').first().text();
			// console.log(file);
			CORE.Ajax({
					module_name: module_name,
					file_name: file_name,
					lang: lang,
					module: transLangFile.module,
					language: CORE.language,
					event: 'file'
				},
				function(res) {
					if (typeof(res) == 'Object') {
						var html = res.data;
						var count = res.data;
					} else {
						var html = res['data'];
						var count = res['count'];
					}
					file.attr('src', transLangFile.url +'images/icons/minus.gif');
					tr.parent().css('height', count * 101 + tr.parent().outerHeight()).attr('valign', 'top');
					div_lng.parent().first().css('height', count * 106);
					file.parents('div').first().css('height', count * 102).css('margin', 2).css('width', 805);
					file.parents('div').first().append(html);
				}
			);
		}
	},
	UpdateFileLang: function (module_v, file) {
		var self = jQuery(this);
		var form = jQuery('#' + module_v); 
		var key = self.parent().find('input[name="key[]"]');
		var val = self.parent().find('textarea[name="text[]"]');
		// var key = form.find('input[name="key[]"]');
		// var val = form.find('textarea[name="text[]"]');
		var val_v = [];
		var key_v = [];
		key.each(function(i, v) {
			key_v[i] = $(v).val();
		});
		val.each(function(j, va) {
			val_v[j] = $(va).val();
		});
		CORE.Ajax (
			{
				module: transLangFile.module,
				module_name: module_v,
				file: file,
				language: CORE.language,
				event: 'save',
				key: key_v,
				val: val_v
			},
			function(res) {
				//TODO Alert
			}
		);
	},
	addNewRow: function (module_name) {
		var that = jQuery('#'+ module_name);
		tr = that.find('.trans_item').last();
		// console.log(tr);
		tr.clone(false).find("input[type=text], textarea").val("").removeAttr('readonly').end().insertAfter(tr);
		that.css('height', 91 + that.outerHeight());
		// div_lng.parent().first().css('height', count * 96);
		that.closest('.trans_file').css('height', that.closest('.trans_file').outerHeight() + 92);
		that.closest('.trans_lang').css('height', that.closest('.trans_lang').outerHeight() + 96);
		that.closest('td').css('height', that.closest('td').outerHeight() + 96);
	}
};